package br.edu.ufabc.gtrnp.helppo.business;

import static br.edu.ufabc.gtrnp.helppo.util.RPCOpenSimMethods.ADMIN_LOAD_OAR;
import static br.edu.ufabc.gtrnp.helppo.util.RPCOpenSimParameters.FILE_NAME;
import static br.edu.ufabc.gtrnp.helppo.util.RPCOpenSimParameters.REGION_NAME;
import static br.edu.ufabc.gtrnp.helppo.util.RPCOpenSimParameters.SERVER_PASSWORD;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;

import br.edu.ufabc.gtrnp.helppo.business.exception.CargaOarRPCException;
import br.edu.ufabc.gtrnp.helppo.business.exception.NenhumEspacoDisponivelException;
import br.edu.ufabc.gtrnp.helppo.util.HelppoProperties;
import br.edu.ufabc.gtrnp.helppo.util.RpcClientSingleton;

public class ArquivoOARBusiness {
	private static ArquivoOARBusiness business = new ArquivoOARBusiness();
	private String nomeRegiao;
	private String ambiente;

	private ArquivoOARBusiness() {
	}

	public static ArquivoOARBusiness getInstance() {
		return business;
	}

	public void carregar(String nomeRegiao, String ambiente)
			throws IOException, NenhumEspacoDisponivelException, CargaOarRPCException {
		this.nomeRegiao = nomeRegiao;
		this.ambiente = ambiente;

		XmlRpcClient client = RpcClientSingleton.getInstance();
		try {
			client.execute(ADMIN_LOAD_OAR, parameter());
		} catch (XmlRpcException e) {
			e.printStackTrace();
			throw new CargaOarRPCException();
		}

	}

	private List<Hashtable<String, Object>> parameter() throws IOException,
			NenhumEspacoDisponivelException {
		Hashtable<String, Object> parameters = new Hashtable<String, Object>();

		ResourceBundle properties = HelppoProperties.getInstance();

		parameters.put(FILE_NAME, properties.getString(ambiente));
		parameters.put(REGION_NAME, nomeRegiao);
		parameters.put(SERVER_PASSWORD, properties.getString(SERVER_PASSWORD));

		List<Hashtable<String, Object>> list = new ArrayList<Hashtable<String, Object>>();
		list.add(parameters);

		return list;
	}

}
